from time import sleep

from base.box import BoxDriver, Browser, TestCase
from biz.ranzhi.main_page import MainPage


class RanzhiTest(TestCase):
    """
    用例 [rzat01]  系统登录模块能接受不同的语言和不同类型的用户在有效和无效范围内登录。
    """

    base_url = None
    main_page = None

    def set_up(self):
        """
        测试前置条件
        :param **kwargs:
        :return:
        """
        self.logger.info("开始测试前置条件")
        self.base_driver = BoxDriver(Browser.Chrome)
        self.base_url = "http://192.168.1.251/ranzhi/www/"
        self.main_page = MainPage(self.base_driver, self.logger)
        self.logger.info("完成测试前置条件")

    def tear_down(self):
        """
        测试清理条件
        :return:
        """
        self.base_driver.quit()

    def test_01(self):
        """
        测试有效和无效用户登录
        :return:
        """
        csv_data = self.read_csv_as_dict("./case/data/ranzhi_test.csv")

        for row in csv_data:

            account = row["用户名"].strip()
            password = row["密码"].strip()
            if row["保持登录"].strip() == "1":
                keep = True
            else:
                keep = False

            lang = row["语言"].strip()
            expected_lang_text = row["期待语言按钮"].strip()
            expected_login_text = row["期待登录按钮"].strip()
            expected_fail_message = row["失败登录提示"].strip()

            if row["是否有效等价"].strip() == "1":
                is_valid_login = True
            else:
                is_valid_login = False

            self.main_page.open(self.base_url)

            self.main_page.change_language("zh_TW")

            # 这里对当前步骤进行截图
            # self.images.append(self.base_driver.save_window_snapshot_by_io())
            self.snapshot()

            # 添加日志
            # self.logger.info("刚刚进行了繁体语言切换截图")
            self.log("刚进行了新的日志编写，切换繁体")
            self.main_page.change_language(lang)

            # 检查语言
            actual_text_dict = self.main_page.get_text_dict_for_language()
            self.assertEqual(expected_lang_text, actual_text_dict["lang"], "切换语言后，语言按钮文字不匹配！")
            self.assertEqual(expected_login_text, actual_text_dict["login"], "切换语言后，登录按钮文字不匹配！")

            actual_keep_checked = self.main_page.login_and_submit(account, password, keep)

            sleep(2)
            # 检查 复选框 勾选情况
            if keep:
                expected_keep_checked = "true"
            else:
                expected_keep_checked = None

            url_before_login = self.base_driver.get_url()
            self.assertEqual(expected_keep_checked, actual_keep_checked, "用户[%s]【保持登录】按钮的勾选验证失败！" % account)

            # 根据是否有效等价检查登录
            if is_valid_login:
                expected_url = self.base_url + "sys/index.html"
                actual_url = self.base_driver.get_url()
                self.assertEqual(expected_url, actual_url, "有效登录用户[%s]登录失败或者登录后跳转错误！" % account)

                self.main_page.do_logout_by_lang(lang)
                # 检查登出
                expected_url = self.base_url + "sys/user-login.html"
                actual_url = self.base_driver.get_url()
                self.assertEqual(expected_url, actual_url, "有效登录用户[%s]退出失败或者退出后跳转错误！" % account)

            else:
                expected_url = url_before_login
                actual_url = self.base_driver.get_url()
                self.assertEqual(expected_url, actual_url, "无效登录用户[%s]登录页面跳转错误！" % account)

                # 检查失败提示文字
                actual_fail_message = self.main_page.get_fail_login_message()
                self.assertIn(expected_fail_message, actual_fail_message, "无效登录用户[%s]登录页面文字提示错误！" % account)

    def test_02(self):
        """
        测试选择 app
        :return:
        """
        self.log("[RanzhiTest]开始 test_02")
        self.main_page.open(self.base_url)
        self.main_page.input_login_info("admin", "123456")

        self.main_page.do_login()

        self.main_page.select_app("oa")
        self.main_page.select_app("crm")
        self.main_page.select_app("cash")
        self.main_page.select_app("proj")
        self.log("[RanzhiTest]结束 test_02")
